using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Jasongozy.Alaton;
using System.Text.RegularExpressions;
using Jasongozy.Copor.Utility;
namespace Jasongozy.Copor {
    /// <summary>
    /// parse the template xml to TemplateTreeNode tree
    /// </summary>
    public class TemplateNodeDefaultParser : ITemplateParser {

        private Dictionary<string, string> _macros = new Dictionary<string, string>();
        
        private Dictionary<string, XmlNode> _funcNodes = new Dictionary<string, XmlNode>();

        public TemplateTreeNode Parse(string filePath) {
            XmlDocument doc = new XmlDocument();
            try {
                doc.Load(filePath);
            } catch {
                
            }
            return Parse(doc);
        }

        #region ITemplateParser Members

        public TemplateTreeNode Parse(XmlDocument doc) {
            LoadMacros(doc);
            LoadFuncs(doc);
            XmlNode rootNode = doc.DocumentElement.SelectSingleNode("TemplateItem");
            return ParseTreeNode(rootNode);
            //throw new Exception("The method or operation is not implemented.");
        }

        #endregion

        private void LoadMacros(XmlDocument doc) {
            XmlNode rootNode = doc.DocumentElement.SelectSingleNode("Macros");
            if (rootNode == null || !rootNode.HasChildNodes) return;
            foreach (XmlNode n in rootNode.ChildNodes) {
                string name = XmlTool.GetValue(n, "@Name");
                if (string.IsNullOrEmpty(name)) continue;
                _macros[name] = n.InnerText.Trim();
            }
        }
        private void LoadFuncs(XmlDocument doc) {
            XmlNode rootNode = doc.DocumentElement.SelectSingleNode("Functions");
            foreach (XmlNode n in rootNode.ChildNodes) {
                string name = XmlTool.GetValue(n, "@Name");
                if (string.IsNullOrEmpty(name)) continue;
                _funcNodes[name] = n;
            }
        }





        private TemplateTreeNode ParseTreeNode(XmlNode rootNode) {
            TemplateTreeNode item = new TemplateTreeNode();
            item.TemplateNodeName = XmlTool.GetValue(rootNode, "@Name") ?? "";
            item.HasRegion = TypeParser.ToBool(XmlTool.GetValue(rootNode, "@HasRegion"), false);
            item.FilePath = XmlTool.GetValue(rootNode, "@FilePath") ?? null;
            item.SourceFilePath = XmlTool.GetValue(rootNode, "@Source") ?? null;
            string content = XmlTool.GetValue(rootNode, "Content");
            if (!string.IsNullOrEmpty(content)) {
                content = TemplareParseHelper.ReplaceMacro(content,_macros);
                TemplareParseHelper.LoadFuncAndProperty(item, content, _funcNodes,_macros);
                item.TemplateContent = content;
            }
            XmlNodeList childTemplates = rootNode.SelectNodes("TemplateItem");
            if (childTemplates != null && childTemplates.Count != 0) {
                foreach (XmlNode subNode in childTemplates) {
                    TemplateTreeNode subTreeNode = ParseTreeNode(subNode);
                    subTreeNode.Parent = item;
                    item.ChildNodes.Add(subTreeNode);
                }
            }

            return item;
        }



       


    }
}
